<?php
/**
 * Created by PhpStorm.
 * User: kwibon
 * Date: 2018/11/26
 * Time: 11:18
 */
namespace app\api\controller;

use app\common\controller\Api;
class KbxToken extends Api
{
    protected $model = null;
//    public  $url='https://n.kuaibangxing.com/api';
//    public  $bSellerBackendUrl='https://b.kuaibangxing.com/sellerBackend/api';
//    public  $sellerBackendUrl='https://n.kuaibangxing.com/sellerBackend/api';
    public  $url='https://d.kwibon.cn/api';//测试地址
    public  $bSellerBackendUrl='https://b.kwibon.cn/sellerBackend/api';//测试地址
    public  $sellerBackendUrl='https://d.kwibon.cn/sellerBackend/api';//测试地址

    public function post_data($url, $data_string,$token) {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HEADER, TRUE);		//表示需要response header
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
                "Content-Type: application/json; charset=utf-8",
                'Authorization:'.$token,
            )
        );
        ob_start();
        curl_exec($ch);
        $return_content = ob_get_contents();
        ob_end_clean();
        //获取头部
        $header_size	= curl_getinfo($ch, CURLINFO_HEADER_SIZE);
        $return_content = substr($return_content,$header_size);

        return $return_content;
    }

    public function get_data($url,$token){
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'GET');
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HTTPHEADER, array(
            "Content-Type: application/json; charset=utf-8",
            'Authorization:'.$token
        ));
        curl_setopt($curl, CURLOPT_FAILONERROR, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_HEADER, 0);
        $output = curl_exec($curl);
        $info = json_decode($output, true);
        return $info;
    }

    public function http_post_data($url, $data_string) {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HEADER, TRUE);		//表示需要response header

        curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
                "Content-Type: application/json; charset=utf-8",
                "Content-Length: " . strlen($data_string))
        );
        ob_start();
        curl_exec($ch);
        $return_content = ob_get_contents();
        ob_end_clean();

        //获取头部
        $header_size	= curl_getinfo($ch, CURLINFO_HEADER_SIZE);
        $headers		= substr($return_content, 0, $header_size);
        $return_content = substr($return_content,$header_size);
        //http状态码
        $return_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);

        return array($return_code, $headers,$return_content);
    }

    public function sendCurl($url, $method = 'GET', $headers = [], $data = '')
    {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
        if (!empty($data)) {
            $sendUrl = $url . '?token=' . $data['token'].'&unionId='.$data['unionId'];
        } else {
            $sendUrl = $url;
        }
        curl_setopt($curl, CURLOPT_URL, $sendUrl);
        curl_setopt($curl, CURLOPT_HTTPHEADER, array(
            "Content-Type: application/json; charset=utf-8"
        ));
        curl_setopt($curl, CURLOPT_FAILONERROR, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_HEADER, 0);
        $urlArr = explode('/',$url);
        $host = $urlArr[0] . '//'. $urlArr[2];
        if (1 == strpos("$" . $host, "https://")) {
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        }
        $output = curl_exec($curl);
        $info = json_decode($output, true);
        return $info;
    }

    public function object_array($array) {
        if(is_object($array)) {
            $array = (array)$array;
        }
        if(is_array($array)) {
            foreach($array as $key=>$value) {
                $array[$key] = $this->object_array($value);
            }
        }
        return $array;
    }



    /**
     * 快帮行Token获取
     *
     * @param $clientId 客户端ID，由快帮行后台提前分配
     * @param $secret   根据当前时间生成的时间字符串
     * @param $timeStamp    快帮行后台提供的 秘钥+timeStamp 字符串合并后做MD5加密的结果字符串
     * $clientId='5b4f0a2322437c15382e767d',$secret='kwiBon20l8',$timeStamp=''
     */
    public function kbxToken($clientId='5b4f0a2322437c15382e767d',$secret='kwiBon20l8'){
        $url = $this->url."/auth/login/client";
        $timeStamp = date("YmdH:i",time());
        $data['clientId'] = $clientId;
        $data['timeStamp'] = $timeStamp;
        $data['secret'] = md5($secret.$data['timeStamp']);
        $data = json_encode($data);
        list($return_code,$headers,$return_content) = $this->http_post_data($url, $data);
        $content = json_decode($return_content,true);

        if($content['token']){
            return $content['token'];
        }else{
            $this->error(__('Server error'), '', -104);
        }
    }



    /**
     * 快帮行用户详情获取
     *
     * @param $id   用户id
     * @param string $phone
     * @param string $unionId
     */
    public function kbxUser($username='',$id='',$unionId=''){
        $url = $this->url."/client/users/getUser?";
        if(!empty($username)){
            $url=$url.'username='.urlencode($username).'&';
        }
        if (!empty($unionId)){
            $url=$url."unionidOA=$unionId&";
        }
        if(!empty($id)){
            $url=$url.'id='.$id;
        }

        $token = 'Bearer '.$this->kbxToken();
        $content = $this->post_data($url,'',$token);
        $content=$this->object_array(json_decode($content));

        return  $content;
    }




}